package com.amkai.aes.webservice.binary.objecthandler;

import java.io.Serializable;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.amkai.aes.aescommon.service.binary.job.acsupport.ACLoginSupportGetterJob;
import com.amkai.aes.aescommon.service.result.acsupport.ACLoginSupportGetterResult;
import com.amkai.aes.generatedbeans.passport.ACLoginSupport;
import com.amkai.aes.webservice.binary.JobObjectHandler;
import com.amkai.common.extensions.hibernate.config.SiteManager;
import com.amkai.common.extensions.hibernate.config.manager.SessionManager;

public class ACLoginSupportListHandler implements JobObjectHandler<ACLoginSupportGetterJob, Serializable, ACLoginSupportGetterResult> {


	
	@SuppressWarnings("unchecked")
	public ACLoginSupportGetterResult process(ACLoginSupportGetterJob job, Serializable object) throws Exception {
		SessionManager sm = SiteManager.Start();
		
		try {
			sm.openSession();
//			String query = "SELECT s FROM ACLoginSupport s " +
//							"LEFT JOIN FETCH s.physStaff " +
//							"LEFT JOIN FETCH s.physStaff.specialty " +
//							"WHERE s.passport = ?";
			//#12575
			String query = "SELECT s FROM ACLoginSupport s " +
							"LEFT JOIN FETCH s.physStaff ps " +
							"LEFT JOIN FETCH ps.baseOrganization bo " +
							"LEFT JOIN FETCH bo.parent bg " +
							"LEFT JOIN FETCH ps.specialty " +
							"WHERE (s.passport = :pass) AND ( bg.id = :bgid)";
			List<ACLoginSupport> supports = (List<ACLoginSupport>) sm.execute(query).setLong("pass", job.getPassportID()).setLong("bgid",job.getModuleDepartment().getParent().getId()).list();
			for (ACLoginSupport s : supports) {
				s.getDepartment().getName();
				s.getPhysStaff().getName();
				if (s.getPhysStaff().getSpecialty() != null) {
					s.getPhysStaff().getSpecialty().getName();
				}
			}
			ACLoginSupportGetterResult result = new ACLoginSupportGetterResult();
			result.setSupportObjects(supports);
			return result;
		} catch (Exception e) {
			throw e;
		}
		finally {
			sm.closeSession();
		}
	}

}
